#!/usr/bin/python
#coding=utf-8
################################
# @Author 
# @Date 2010-7-
################################
# @FileName 剪贴板 for eMule
# @Version 
################################
#!/usr/bin/python

"""
Watchmen: Your system watcher

Watching system clipboard for various kind of events

CopyRight (C) 2009 Chen Zheng <nkchenz@gmail.com> 
Distributed under terms of GPL v2
"""

import gtk
import urllib
import re
import os

def ed2k_handler(links):
    """Queue the link"""
    for link in links:
        link = urllib.unquote(link)
        print link
        os.system('ed2k "%s"' % link)

def _clipboard_changed(clipboard, event):
    text = clipboard.wait_for_text()
    print text
    if text:
        plugins_routing(text)

def plugins_routing(text):
    for p in plugins:
        matches = re.findall(p['pattern'], text.replace('\n', ''))
        if not matches:
            continue
        print p['name'], 'Found'
        p['handler'](matches)
        return True
    return False

#clip = gtk.clipboard_get(gtk.gdk.SELECTION_PRIMARY)
#clip.connect("owner-change", _clipboard_changed)
#clip = gtk.clipboard_get(gtk.gdk.SELECTION_SECONDARY)
#clip.connect("owner-change", _clipboard_changed)
clip = gtk.clipboard_get(gtk.gdk.SELECTION_CLIPBOARD)
clip.connect("owner-change", _clipboard_changed)

plugins = []
plugins.append({'name': 'ed2k', 'pattern': 'ed2k://.*?/', 'handler': ed2k_handler})

gtk.main()

